/*
 * Copyright (c) 2024 endless-sky
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "peripheral.h"
#include "peripheral_uart.h"
#include "peripheral_switch_control.h"
#include "utils_log.h"

namespace El {
namespace Peripheral {

PeripheralService::PeripheralService()
    : started_(false)
{
}

PeripheralService::~PeripheralService()
{
    Stop();
}

PeripheralService &PeripheralService::GetInstance()
{
    static PeripheralService instance;
    return instance;
}

bool PeripheralService::Start()
{
    std::lock_guard<std::mutex> lock(mutex_);

    if (started_) {
        EL_DEBUG("Peripheral service already started");
        return true;
    }

    try {
        EL_INFO("Starting peripheral service...");

        // 1. 启动串口管理器
        EL_DEBUG("Starting UART manager...");
        if (!UartManager::GetInstance().Start()) {
            EL_ERROR("Failed to start UART manager");
            return false;
        }
        EL_DEBUG("UART manager started successfully");

        // 2. 启动开关控制器
        EL_DEBUG("Starting switch controller...");
        if (!SwitchControl::GetInstance().Start()) {
            EL_WARN("Failed to start switch controller, continuing");
        } else {
            EL_DEBUG("Switch controller started successfully");
        }
        
        started_ = true;
        EL_INFO("Peripheral service started");
        return true;

    } catch (const std::exception &e) {
        EL_ERROR("Peripheral service start exception: {}", e.what());
        return false;
    } catch (...) {
        EL_ERROR("Peripheral service start unknown exception");
        return false;
    }
}

bool PeripheralService::Stop()
{
    std::lock_guard<std::mutex> lock(mutex_);

    if (!started_) {
        EL_DEBUG("Peripheral service not started");
        return true;
    }

    try {
        EL_INFO("Stopping peripheral service...");

        // 2. 停止开关控制器
        EL_DEBUG("Stopping switch controller...");
        if (!SwitchControl::GetInstance().Stop()) {
            EL_WARN("Failed to stop switch controller");
        }

        // 3. 停止串口管理器
        EL_DEBUG("Stopping UART manager...");
        if (UartManager::GetInstance().DeinitAll() != 0) {
            EL_WARN("Failed to stop UART manager");
        }

        started_ = false;
        EL_INFO("Peripheral service stopped");
        return true;

    } catch (const std::exception &e) {
        EL_ERROR("Peripheral service stop exception: {}", e.what());
        return false;
    } catch (...) {
        EL_ERROR("Peripheral service stop unknown exception");
        return false;
    }
}

bool PeripheralService::IsStarted() const
{
    std::lock_guard<std::mutex> lock(mutex_);
    return started_;
}

} // namespace Peripheral
} // namespace El
